package com.helloscala.helper

import java.sql.{DriverManager, Connection}

import org.squeryl.{Session, SessionFactory}
import org.squeryl.adapters.PostgreSqlAdapter
import org.squeryl.internals.DatabaseAdapter

object RdbHelpers {
  Class.forName("org.postgresql.Driver")

  def registerSquerylSession() {
    SessionFactory.concreteFactory = Some(() => defaultSession)
  }

  def defaultSession: Session = {
    val db = HelloHelpers.config.dbs.head
    session(db.url, db.username, db.password, new PostgreSqlAdapter)
  }

  def session(url: String, username: String, password: String, adapter: DatabaseAdapter): Session =
    Session.create(getConnection(url, username, password), adapter)

  def getConnection(url: String, username: String, password: String): Connection =
    DriverManager.getConnection(url, username, password)

  def close() {}
}
